home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC_Samples / cbartest / cbartest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  8.0 KB  |  333 lines

  1. // CBARTEST.CPP
  2. //
  3. // Description:  This sample application demonstrates the use of the
  4. // CCommandBar class, which has been added specifically for WCE
  5. //
  6. //
  7. // This is a part of the Microsoft Foundation Classes C++ library.
  8. // Copyright (C) 1999 Microsoft Corporation
  9. // All rights reserved.
  10. //
  11. // This source code is only intended as a supplement to the
  12. // Microsoft Foundation Classes Reference and related
  13. // electronic documentation provided with the library.
  14. // See these sources for detailed information regarding the
  15. // Microsoft Foundation Classes product.
  16.  
  17.  
  18. #include "stdafx.h"
  19. #include "resource.h" 
  20.  
  21. //////////////////////////////////////////////////////////////////////////
  22. //  Declarations
  23. //////////////////////////////////////////////////////////////////////////
  24.  
  25. class CMainWindow : public CFrameWnd
  26. {
  27. private:
  28.     BOOL UpdateCommandBar(BOOL);
  29.     void CheckMenuItems(CMenu*);
  30.  
  31.     BOOL m_bCombo;
  32.     BOOL m_bToolbar;
  33.     BOOL m_bHelp;
  34.     BOOL m_bOK;
  35. #if (_WIN32_WCE > 200)
  36.     CCeCommandBar m_wndCommandBar;
  37. #endif
  38.  
  39. public:
  40.     CMainWindow();
  41.     ~CMainWindow() {};
  42.  
  43.     //{{AFX_MSG( CMainWindow )
  44.     afx_msg void OnPaint();
  45.     afx_msg void OnFileExit();
  46.     afx_msg void OnAbout();
  47.     afx_msg void OnEditCut();
  48.     afx_msg void OnEditCopy();
  49.     afx_msg void OnEditPaste();
  50.     afx_msg void OnEditUndo();
  51.     afx_msg void OnOptionsCombo();
  52.     afx_msg void OnOptionsToolbar();
  53.     afx_msg void OnOptionsHelp();
  54.     afx_msg void OnOptionsOk();
  55.     afx_msg void OnOK();
  56.     afx_msg void OnHelpInfo(HELPINFO *pHelpInfo);
  57.     //}}AFX_MSG
  58.  
  59.     DECLARE_MESSAGE_MAP()
  60. };
  61.  
  62. class CCmdBarApp : public CWinApp
  63. {
  64. public:
  65.     virtual BOOL InitInstance()
  66.     {
  67.         m_pMainWnd = new CMainWindow();
  68.         m_pMainWnd->ShowWindow( m_nCmdShow );
  69.         m_pMainWnd->UpdateWindow();
  70.         return TRUE;
  71.     }
  72. } theApp;
  73.  
  74.  
  75. CMainWindow::CMainWindow()
  76. {
  77.     m_bCombo = TRUE;
  78.     m_bToolbar = TRUE;
  79.     m_bToolbar = TRUE;
  80.     m_bOK = TRUE;
  81.  
  82.     LoadFrame(IDR_MAINFRAME);
  83.     UpdateCommandBar(FALSE);
  84. }
  85.  
  86. void CMainWindow::OnPaint()
  87. {
  88.     CRect rect;
  89.     GetClientRect(rect);
  90.  
  91.     CPaintDC dc(this);
  92.     dc.DrawText(CString((LPCTSTR)IDS_HELLO), rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  93. }
  94.  
  95.  
  96. #if (_WIN32_WCE > 200)
  97.  
  98. // MFCCE 2.01 and later uses CCeCommandBar, a derivitive of CToolBar, to implement command bars. 
  99.  
  100. BOOL CMainWindow::UpdateCommandBar(BOOL bReset)
  101. {
  102.     BOOL bOk = TRUE;
  103.  
  104.     if(m_wndCommandBar.m_hWnd == NULL)
  105.         bOk = m_wndCommandBar.Create(this);
  106.  
  107.     if(bOk)
  108.     {
  109.         if(bReset)
  110.             m_wndCommandBar.ResetCommandBar();
  111.  
  112.         CMenu *pMenu = m_wndCommandBar.InsertMenuBar(IDR_MAINFRAME);
  113.         CheckMenuItems(pMenu);
  114.  
  115. #if defined(_WIN32_WCE_PSPC)
  116.         bOk = m_wndCommandBar.AddAdornments(m_bOK*CMDBAR_OK | m_bHelp*CMDBAR_HELP);
  117. #else
  118.         bOk = m_wndCommandBar.InsertSeparator(6);
  119. #endif
  120.     }
  121.  
  122.     if(m_bCombo && bOk)
  123.     {
  124.         CComboBox* pComboBox = m_wndCommandBar.InsertComboBox(100, 1001);
  125.         bOk = (pComboBox != NULL);
  126.         if(bOk)
  127.         {
  128.             pComboBox->AddString(CString((LPCTSTR)IDS_ONE));
  129.             pComboBox->AddString(CString((LPCTSTR)IDS_TWO));
  130.             pComboBox->AddString(CString((LPCTSTR)IDS_THREE));
  131.             pComboBox->AddString(CString((LPCTSTR)IDS_FOUR));
  132.             pComboBox->SetCurSel(0);
  133.             bOk = m_wndCommandBar.InsertSeparator(6);
  134.         }
  135.     }
  136.  
  137.     if(m_bToolbar && bOk)
  138.         bOk = m_wndCommandBar.LoadToolBar(IDR_MAINFRAME);
  139.  
  140. #if !defined(_WIN32_WCE_PSPC)
  141.     if(bOk)
  142.         bOk = m_wndCommandBar.AddAdornments(m_bOK*CMDBAR_OK | m_bHelp*CMDBAR_HELP);
  143. #endif
  144.  
  145.     return bOk;
  146. }
  147.  
  148. #else
  149.  
  150. // MFCCE 2.0 and earlier handled command bars through special members of CFrameWnd.
  151. // Do not use this function for MFCCE 2.01 and later.
  152.  
  153. BOOL CMainWindow::UpdateCommandBar(BOOL bReset)
  154. {
  155.     // Note: At this point, CFrameWnd has a command bar    
  156.     if(bReset)
  157.     {
  158.         ResetCommandBar(); // start over with the command bar
  159.         m_hCommandBarMenu = InsertMenu(IDR_MAINFRAME);
  160.     }
  161.     CheckMenuItems(GetMenu());
  162.  
  163.     if(m_bCombo)
  164.     {
  165.         InsertComboBox( 100 );
  166.         AddComboBoxString( CString((LPCTSTR)IDS_ONE) );
  167.         AddComboBoxString( CString((LPCTSTR)IDS_TWO) );
  168.         AddComboBoxString( CString((LPCTSTR)IDS_THREE) );
  169.         AddComboBoxString( CString((LPCTSTR)IDS_FOUR) );
  170.         SetComboCurSel(0);
  171.     }
  172.  
  173.     if(m_bToolbar)
  174.     {
  175.         static TBBUTTON buttons[] = {
  176.             { 0,    0,                TBSTATE_ENABLED, TBSTYLE_SEP,        0, 0, 0, -1},
  177.             { 0,    ID_EDIT_CUT,    TBSTATE_ENABLED, TBSTYLE_BUTTON,    0, 0, 0,  0},
  178.             { 1,    ID_EDIT_COPY,    TBSTATE_ENABLED, TBSTYLE_BUTTON,    0, 0, 0,  1},
  179.             { 2,    ID_EDIT_PASTE,    TBSTATE_ENABLED, TBSTYLE_BUTTON,    0, 0, 0,  2},
  180.             { 0,    0,                TBSTATE_ENABLED, TBSTYLE_SEP,        0, 0, 0, -1},
  181.             { 3,    ID_EDIT_UNDO,    TBSTATE_ENABLED, TBSTYLE_BUTTON,    0, 0, 0,  3},
  182.             { 4,    ID_APP_ABOUT,    TBSTATE_ENABLED, TBSTYLE_BUTTON,    0, 0, 0,  3},
  183.             { 0,    0,                TBSTATE_ENABLED, TBSTYLE_SEP,        0, 0, 0, -1}};
  184.         InsertButtons( buttons, sizeof(buttons)/sizeof(TBBUTTON), IDR_MAINFRAME, 5 );
  185.     }
  186.  
  187.     AddAdornments(m_bOK*CMDBAR_OK | m_bHelp*CMDBAR_HELP);
  188.  
  189.     return TRUE;
  190. }
  191.  
  192. #endif // _WIN32_WCE <= 200
  193.  
  194.  
  195. void CMainWindow::CheckMenuItems(CMenu* pMenu)
  196. {
  197.     pMenu->CheckMenuItem(ID_OPTIONS_COMBO,   (m_bCombo   ? MF_CHECKED : MF_UNCHECKED));
  198.     pMenu->CheckMenuItem(ID_OPTIONS_TOOLBAR, (m_bToolbar ? MF_CHECKED : MF_UNCHECKED));
  199.     pMenu->CheckMenuItem(ID_OPTIONS_OK,      (m_bOK   ? MF_CHECKED : MF_UNCHECKED));
  200.     pMenu->CheckMenuItem(ID_OPTIONS_HELP,    (m_bHelp ? MF_CHECKED : MF_UNCHECKED));
  201. }
  202.  
  203.  
  204.  
  205. BEGIN_MESSAGE_MAP( CMainWindow, CFrameWnd )
  206.     //{{AFX_MSG_MAP( CMainWindow )
  207.     ON_WM_PAINT()
  208.     ON_COMMAND(ID_FILE_EXIT, OnFileExit)
  209.     ON_COMMAND(ID_APP_ABOUT, OnAbout)
  210.     ON_COMMAND(ID_EDIT_CUT, OnEditCut)
  211.     ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
  212.     ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
  213.     ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
  214.     ON_COMMAND(ID_OPTIONS_COMBO, OnOptionsCombo)
  215.     ON_COMMAND(ID_OPTIONS_TOOLBAR, OnOptionsToolbar)
  216.     ON_COMMAND(ID_OPTIONS_HELP, OnOptionsHelp)
  217.     ON_COMMAND(ID_OPTIONS_OK, OnOptionsOk)
  218.     ON_COMMAND(IDOK, OnOK)
  219.     ON_WM_HELPINFO()
  220.     //}}AFX_MSG_MAP
  221. END_MESSAGE_MAP()
  222.  
  223. void CMainWindow::OnFileExit()
  224. {
  225.     SendMessage(WM_CLOSE);
  226. }
  227.  
  228. void CMainWindow::OnOptionsCombo() 
  229. {
  230.     m_bCombo ^= 1;
  231.     UpdateCommandBar(TRUE); 
  232. }
  233.  
  234. void CMainWindow::OnOptionsToolbar() 
  235. {
  236.     m_bToolbar ^= 1;
  237.     UpdateCommandBar(TRUE); 
  238. }
  239.  
  240. void CMainWindow::OnOptionsHelp() 
  241. {
  242.     m_bHelp ^= 1;
  243.     UpdateCommandBar(TRUE);
  244. }
  245.  
  246. void CMainWindow::OnOptionsOk() 
  247. {
  248.     m_bOK ^= 1;
  249.     UpdateCommandBar(TRUE); 
  250. }
  251.  
  252. void CMainWindow::OnEditCut()
  253. {    
  254.     // Display message box to confirm button pressed
  255.     MessageBox(CString((LPCTSTR)ID_EDIT_CUT), CString((LPCTSTR)IDS_TOOLBAR));
  256. }
  257.  
  258. void CMainWindow::OnEditCopy()
  259. {
  260.     // Display message box to confirm button pressed
  261.     MessageBox(CString((LPCTSTR)ID_EDIT_COPY), CString((LPCTSTR)IDS_TOOLBAR));
  262. }
  263.  
  264. void CMainWindow::OnEditPaste()
  265. {
  266.     // Display message box to confirm button pressed
  267.     MessageBox(CString((LPCTSTR)ID_EDIT_PASTE), CString((LPCTSTR)IDS_TOOLBAR));
  268. }
  269.  
  270. void CMainWindow::OnEditUndo()
  271. {
  272.     // Display message box to confirm button pressed
  273.     MessageBox(CString((LPCTSTR)ID_EDIT_UNDO), CString((LPCTSTR)IDS_TOOLBAR));
  274. }
  275.  
  276. void CMainWindow::OnOK()
  277. {
  278.     // Display message box to confirm button pressed
  279.     MessageBox(CString((LPCTSTR)IDS_OK_ADORNMENT), CString((LPCTSTR)IDS_TOOLBAR));
  280. }
  281.  
  282. void CMainWindow::OnHelpInfo(HELPINFO *pHelpInfo)
  283. {
  284.     // Display message box to confirm button pressed
  285.     MessageBox(CString((LPCTSTR)IDS_HELP_ADORNMENT), CString((LPCTSTR)IDS_TOOLBAR));
  286. }
  287.  
  288.  
  289.  
  290. //////////////////////////////////////////////////////////////////////////
  291. //  About dialog
  292. //////////////////////////////////////////////////////////////////////////
  293.  
  294. class CAboutDlg : public CDialog
  295. {
  296. public:
  297.     CAboutDlg( UINT uiResourceID ) : CDialog( uiResourceID )    {}
  298.  
  299.     //{{AFX_MSG( CAboutDlg )
  300.     afx_msg void OnOK();
  301.     virtual BOOL OnInitDialog();
  302.     //}}AFX_MSG
  303. // Implementation
  304.  
  305.     DECLARE_MESSAGE_MAP()
  306. };
  307.  
  308.  
  309. BEGIN_MESSAGE_MAP( CAboutDlg, CDialog )
  310.     //{{AFX_MSG_MAP( CAboutDlg )
  311.     ON_COMMAND(IDOK, OnOK)
  312.     //}}AFX_MSG_MAP
  313. END_MESSAGE_MAP()
  314.  
  315. void CMainWindow::OnAbout()
  316. {
  317.     CAboutDlg about(IDD_ABOUTBOX);
  318.     about.DoModal();
  319. }
  320.  
  321. void CAboutDlg::OnOK()
  322. {
  323.     CDialog::OnCancel();
  324. }
  325.  
  326. BOOL CAboutDlg::OnInitDialog() 
  327. {
  328.     CDialog::OnInitDialog();
  329.     CenterWindow();    
  330.     return TRUE;  
  331. }
  332.  
  333.